#!/bin/bash

if [ ! -d "$DINGOO_SDK" ]; then
	echo "DINGOO_SDK environment variable not set/valid."
	echo "To fix this temporarily type:"
	echo "	export DINGOO_SDK=`pwd`"
	echo "Edit your cygwin.bat or .bashrc file to set this properly."
	exit 0
fi

if [ ! -d "$MIPSTOOLS" ]; then
	echo "MIPSTOOLS environment variable not set/valid."
	exit 0
fi

if [ -d "$WINDIR" ]; then
	SET nodosfilewarning
	$DINGOO_SDK/tools/mipstools_fix/mipstools_fix
else
	make tools
fi

make install

rm -f $DINGOO_SDK/lib/libgcc.a
cp $MIPSTOOLS/lib/gcc/mipsel-linux/*/libgcc.a $DINGOO_SDK/lib

rm -f $DINGOO_SDK/lib/dingoo.xn
echo "ENTRY(AppMain)
OUTPUT_FORMAT("elf32-tradlittlemips")
SECTIONS
{
    .dingoo 0x80A00000 :
    {
        dingoo.o (.text)
        *(.text*)
        *(.rodata*)
        *(.data*)
        *(.eh_frame*)
        *(.gcc_except_table*)

        . = ALIGN(8);
        __CTOR_LIST__ = ABSOLUTE(.);
        KEEP(*(SORT(.ctors*)))
        __CTOR_END__ = ABSOLUTE(.);
        __DTOR_LIST__ = ABSOLUTE(.);
        KEEP(*(SORT(.dtors*)))
        __DTOR_END__ = ABSOLUTE(.);

        *(.lit8)
        *(.lit4)
        *(.sdata)
    }
    _fbss = .;
    .bss :
    {
        *(.sbss)
        *(.scommon)
        *(.bss*)
        *(COMMON)
    }
    _end = .;
    .export_table :
    {
        KEEP(*(.export_table))
    }
	.export_string :
    {
        KEEP(*(.export_string))
    }
    /DISCARD/ :
    {
        *(*)
    }
}
" > $DINGOO_SDK/lib/dingoo.xn

echo "Dingoo SDK installed."
